<template>
  <div class="app-container">
    <div style="margin: 40px">
    <el-form>
      <el-form-item label="寄售平台审核：">
        <el-tooltip class="item" effect="dark" content="平台审核开关" placement="top-start">
          <el-switch
            v-model="platAuditSwitch"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            active-text="开启"
            inactive-text="关闭"
            @change='platSwitchConfig()'
          ></el-switch>
        </el-tooltip>
      </el-form-item>
      <br>
      <el-form-item label="寄售商户审核：">
        <el-tooltip class="item" effect="dark" content="商户审核开关" placement="top-start">
          <el-switch
            v-model="shopAuditSwitch"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            active-text="开启"
            inactive-text="关闭"
            @change='shopSwitchConfig()'
          ></el-switch>
        </el-tooltip>
      </el-form-item>
      <br>
      <el-form-item label="平台自动审核：">
        <el-tooltip class="item" effect="dark" content="关闭自动审核，将需要人工审核" placement="top-start">
          <el-switch
            v-model="autoAuditSwitch"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            active-text="开启"
            inactive-text="关闭"
            @change='auditSwitchConfig()'
          ></el-switch>
        </el-tooltip>
      </el-form-item>
      <br>
      <el-form-item label="取消寄售限制：">
        <el-input v-model="limitDays" placeholder="输入天数" @change="cancelLimitDays()" style="width: 95px; margin-right: 8px"/>天
      </el-form-item>
    </el-form>
    </div>
  </div>
</template>

<script>

  export default {
    name: '',
    data() {
      return {
        platAuditSwitch: false,
        shopAuditSwitch: false,
        autoAuditSwitch: false,
        limitDays: 0
      }
    },
    mounted () {
      this.getAudit()
      this.getPlat()
      this.getShop()
      this.getDays()
    },
    methods: {
      getAudit () {
        this.$http({
          url: this.$http.adornUrl('/sys/config/value/CONSIGN_AUTO_AUDIT'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign()
          )
        }).then(({data}) => {
          this.autoAuditSwitch = data === 'true'
        })
      },
      getPlat () {
        this.$http({
          url: this.$http.adornUrl('/sys/config/value/CONSIGN_PLAT_AUDIT'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign()
          )
        }).then(({data}) => {
          this.platAuditSwitch = data === 'true'
        })
      },
      getShop () {
        this.$http({
          url: this.$http.adornUrl('/sys/config/value/CONSIGN_SHOP_AUDIT'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign()
          )
        }).then(({data}) => {
          this.shopAuditSwitch = data === 'true'
        })
      },
      getDays () {
        this.$http({
          url: this.$http.adornUrl('/sys/config/value/CONSIGN_CANCEL_LIMIT_DAYS'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign()
          )
        }).then(({data}) => {
          this.limitDays = Number(data)
        })
      },
      /**
       * 自动审核开关
       */
      auditSwitchConfig () {
        this.$confirm(this.autoAuditSwitch ? '是否确认开启寄售商品 自动审核?' : '是否确认关闭寄售商品 自动审核?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let date = {
            paramKey: 'CONSIGN_AUTO_AUDIT',
            paramValue: this.autoAuditSwitch
          }
          this.$http({
            url: this.$http.adornUrl('/sys_config/save/'),
            method: 'post',
            data: this.$http.adornData(date)
          }).then(({data}) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.$message({
                  message: '寄售自动审核' + (this.autoAuditSwitch ? '开启' : '关闭') + '成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => this.getAudit()
                })
              }
            })
          })
        }).catch(() => { this.getAudit() })
      },
      /**
       * 平台审核开关
       */
      platSwitchConfig() {
        this.$confirm(this.platAuditSwitch ? '是否确认开启寄售商品 平台审核?' : '是否确认关闭寄售商品 平台审核?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let date = {
            paramKey: 'CONSIGN_PLAT_AUDIT',
            paramValue: this.platAuditSwitch
          }

          this.$http({
            url: this.$http.adornUrl('/sys_config/save/'),
            method: 'post',
            data: this.$http.adornData(date)
          }).then(({data}) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.$message({
                  message: '寄售自动审核' + (this.platAuditSwitch ? '开启' : '关闭') + '成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => this.getPlat()
                })
              }
            })
          })
        }).catch(() => { this.getPlat() })
      },
      /**
       * 商户审核开关
       */
      shopSwitchConfig() {
        this.$confirm(this.shopAuditSwitch ? '是否确认开启寄售商品 商户审核?' : '是否确认关闭寄售商品 商户审核?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let date = {
            paramKey: 'CONSIGN_SHOP_AUDIT',
            paramValue: this.shopAuditSwitch
          }

          this.$http({
            url: this.$http.adornUrl('/sys_config/save/'),
            method: 'post',
            data: this.$http.adornData(date)
          }).then(({data}) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.$message({
                  message: '寄售自动审核' + (this.shopAuditSwitch ? '开启' : '关闭') + '成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => this.getShop()
                })
              }
            })
          })
        }).catch(() => { this.getShop() })
      },
      cancelLimitDays() {
        var regPos = /^[0-9]\d*$/
        if (regPos.test(this.limitDays)) {
          this.$confirm('确认修改【取消寄售的限制天数】吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let date = {
              paramKey: 'CONSIGN_CANCEL_LIMIT_DAYS',
              paramValue: this.limitDays
            }
            this.$http({
              url: this.$http.adornUrl('/sys_config/save/'),
              method: 'post',
              data: this.$http.adornData(date)
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$message({
                    message: '寄售取消限制天数设置成功',
                    type: 'success',
                    duration: 1500
                    // onClose: () => this.getDays()
                  })
                }
              })
            })
          }).catch(() => { this.getDays() })
        } else {
          this.$message({
            message: '您的输入“' + this.limitDays + '”有误，请输入非负整数',
            type: 'error',
            duration: 1500,
            onClose: () => this.getDays()
          })
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .brand-logo-img {
      width: auto;
      height: auto;
      max-width: 100%;
      max-height: 200px;
    }
  }
</style>
